<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州瑞璞玉数字科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\memberwithdraw\model;

use app\model\BaseModel;
use app\model\member\Withdraw as MemberWithdraw;

/**
 * 会员提现
 */
class Withdraw extends BaseModel
{

    /**
     * 转账
     * @param $condition
     */
    public function transfer($id)
    {
        $withdraw_model = new MemberWithdraw();
        $info_result = $withdraw_model->getMemberWithdrawInfo([ [ "id", "=", $id ] ], "withdraw_no,account_number,realname,money,memo,transfer_type,site_id,applet_type,member_id");
        if (empty($info_result[ "data" ]))
            return $this->error();

        $info = $info_result[ "data" ];
        if (!in_array($info[ "transfer_type" ], [ "wechatpay", "alipay" ]))
            return $this->error('', "当前提现方式不支持在线转账");

        $pay_data = array (
            "id" => $id,
            "out_trade_no" => $info[ "withdraw_no" ],
            "real_name" => $info[ "realname" ],
            "amount" => $info[ "money" ],
            "desc" => "会员提现" . $info[ "memo" ],
            "transfer_type" => $info[ "transfer_type" ],
            "account_number" => $info[ "account_number" ],
            "site_id" => $info[ "site_id" ],
            "is_weapp" => $info[ "applet_type" ],
            "member_id" => $info[ 'member_id' ]
        );
        //调用在线转账借口
        $pay_result = event("PayTransfer", $pay_data, true);
        if (empty($pay_result)) {
            $pay_result = $this->error();
        }
        if ($pay_result[ "code" ] < 0) {
            return $pay_result;
        }
        //调用完成转账
        $result = $withdraw_model->transferFinish([ "id" => $id, "site_id" => $info[ "site_id" ] ]);
        return $result;
    }
}